<?php
/*
 * 重置atacq密码为abc123，如用户不存在则创建
 */
require_once 'config.php';

$conn = dbConnect();
$username = 'atacq';
$password = 'abc123';
$role = 'admin'; // 默认角色
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

// 检查用户是否存在
$checkStmt = $conn->prepare('SELECT id FROM users WHERE username = ?');
$checkStmt->bind_param('s', $username);
$checkStmt->execute();
$checkResult = $checkStmt->get_result();

if ($checkResult->num_rows > 0) {
    // 更新密码
    $stmt = $conn->prepare('UPDATE users SET password = ? WHERE username = ?');
    $stmt->bind_param('ss', $hashedPassword, $username);
    $stmt->execute();
    $message = '密码已重置为: <strong>' . htmlspecialchars($password) . '</strong>';
    $title = '操作成功';
    $icon = 'check-circle';
    $iconColor = 'text-success';
} else {
    // 插入新用户
    $stmt = $conn->prepare('INSERT INTO users (username, password, role) VALUES (?, ?, ?)');
    $stmt->bind_param('sss', $username, $hashedPassword, $role);
    $stmt->execute();
    $message = '用户 <strong>' . htmlspecialchars($username) . '</strong> 已创建，密码为: <strong>' . htmlspecialchars($password) . '</strong>';
    $title = '用户创建成功';
    $icon = 'user-plus';
    $iconColor = 'text-primary';
}

// 关闭语句和连接
$checkStmt->close();
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>密码重置 - 仓库管理系统</title>
    <link rel="stylesheet" href="/dist/css/tabler.min.css">
</head>
<body class="layout-fluid layout-vertical">
    <div class="page">
        <div class="container-fluid">
            <div class="row justify-content-center mt-5">
                <div class="col-md-6 col-lg-4">
                    <div class="card shadow-lg">
                        <div class="card-body p-6 text-center">
                            <div class="mb-4">
                                <svg xmlns="http://www.w3.org/2000/svg" class="icon <?= $iconColor ?> icon-3x" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                    <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
                                    <?php if ($icon === 'check-circle'): ?>
                                    <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
                                    <path d="M9 12l2 2l4 -4" />
                                    <?php else: ?>
                                    <path d="M15 11a3 3 0 1 1 -6 0a3 3 0 0 1 6 0z" />
                                    <path d="M19 11v-2a3 3 0 0 0 -3 -3h-4a3 3 0 0 0 -3 3v6a3 3 0 0 0 3 3h4a3 3 0 0 0 3 -3v-2" />
                                    <path d="M9 17h6" />
                                    <?php endif; ?>
                                </svg>
                            </div>
                            <h1 class="h2 mb-2"><?= $title ?></h1>
                            <p class="text-muted mb-4">
                                <?= $message ?>
                            </p>
                            <div class="mb-4">
                                <div class="progress" style="height: 6px;">
                                    <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 0%" id="redirectProgress"></div>
                                </div>
                            </div>
                            <p class="text-sm text-muted">
                                <span id="countdown">5</span> 秒后自动跳转至登录页面...
                            </p>
                            <div class="mt-5">
                                <a href="login.php" class="btn btn-primary">
                                    立即前往登录
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="/dist/js/tabler.min.js"></script>
    <script>
        // 倒计时功能
        let countdown = 5;
        const countdownElement = document.getElementById('countdown');
        const progressElement = document.getElementById('redirectProgress');
        const interval = setInterval(() => {
            countdown--;
            countdownElement.textContent = countdown;
            progressElement.style.width = `${(5 - countdown) * 20}%`;
            if (countdown <= 0) {
                clearInterval(interval);
                window.location.href = 'login.php';
            }
        }, 1000);
    </script>
</body>
</html>

